<?php
namespace src\logic;
use \classlib\core as C;
class lmanage {
	const LSize = 15;
	
	//存放全局的入口位置
	public static function getuids($sid) {
		$ids   = C\model::load('main.pl.manager')->set_dev($sid)
			->get_column(0, -1, array('studioid'=>$sid), 'userid');
		if (!is_array($ids)) {
			$ids = array();
		}
		return $ids;
	}
	
	//设置一个用户为管理员
	public static function setuid($view, $muserid, $sid, $userid, $ismnger) {
		$lang   = C\lang::inst('web');
		$studio = C\model::load('main.pl.studio')->get_one($sid);
		if (!$studio || $studio['userid'] != $muserid || $muserid < 1) {
			$view->assign_error(80011, $lang->txt('action', '80011'));
		}
		
		$fields = array('userid'=>$userid, 'studioid'=>$sid);
		$sorm   = C\model::load('main.pl.manager')->set_dev($sid);
		$mnger  = $sorm->get_item($fields);
		if ($ismnger == 1 && !$mnger) {
			//直播间房管认识的限制
			$ttl= $sorm->get_total(array('studioid'=>$sid));
			if ($ttl >= self::LSize) {
				$view->assign_error(80070, $lang->txt('action', '80070', self::LSize));
			}
			$fields['stime'] = C\config::$nowtime;
			$sorm->new_one($fields);
		} else if ($ismnger == 0 && $mnger) {
			$sorm->delete($mnger['id']);
			//取消管理员设置的时候消息提醒
			if (C\config::$nowtime - $mnger['stime'] > 300) {
				$user = C\model::load('main.pl.userinfo')->set_dev($muserid)->get_one($muserid);
				$link = C\router::get_url('live', $studio['studiono']);
				$msg  = $lang->txt('action', '80065', $user['nickname'], $link);
				$title= $lang->txt('action', '80064');
				lnews::send($title, $msg, $userid, 3, $muserid);
			}
		}
		//发送广播
		$access = laccess::reset($userid, $sid);
		$data = array('userid'=>$muserid, 'admin'=>$userid, 'access'=>$access, 'ismanager'=>$ismnger, 'setmng'=>1);
		limapi::notifystudio($sid, json_encode($data), 0, 20019);
	}
}